package com.forevershine.business.service;

import java.lang.reflect.ParameterizedType;

import com.forevershine.business.context.ContextBuilder;
import com.forevershine.business.persistent.IDataAccessor;
import com.google.inject.Inject;
import com.google.inject.Injector;

public abstract class AbstractService<X, T>  {
	protected Injector injector;
	protected IDataAccessor mySqlAccessor;
	protected T daoModel;

	@Inject
	protected void init() {
		if (injector == null || mySqlAccessor == null || daoModel == null) {
			injector = ContextBuilder.getContext();
			mySqlAccessor = injector.getInstance(IDataAccessor.class);
			try {

				if (this.getClass().getGenericSuperclass() instanceof ParameterizedType) {
					ParameterizedType pt = (ParameterizedType) this.getClass()
							.getGenericSuperclass();
					Class clazz = (Class) pt.getActualTypeArguments()[1];
					this.daoModel = (T) injector.getInstance(clazz);
				}

			} catch (SecurityException e) {
				e.printStackTrace();
			}
		}

	}


}
